<?php
namespace app\member\controller;
use app\AdminRest;
use app\erp\model\OutOrder;
use app\massage\model\BalanceWater;
use app\massage\model\User;
use app\member\info\PermissionMember;
use app\member\model\Goods;
use app\member\model\Member;
use app\member\model\RightsRelation;
use app\shop\controller\AdminSellingWater;
use longbingcore\wxcore\Excel;
use longbingcore\wxcore\WxSetting;
use think\App;
use app\member\model\Member as model;
use app\member\model\Level;
use app\member\model\Growth;
use app\member\model\Integral;
use app\member\model\StoredOrder;
use think\facade\Db;


class AdminMember extends AdminRest
{

    protected $model;

    protected $level_model;

    protected $user_model;


    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new model();

        $this->level_model = new Level();

        $this->growth_model = new Growth();

        $this->integral_model = new Integral();

        $this->store_order_model = new StoredOrder();

        $this->user_model = new User();

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-15 10:49
     * @功能说明:会员列表
     */

    public function memberList(){

        $input = $this->_param;

       // $this->model->memberInit($this->_uniacid);

        $dis[] = ['uniacid','=',$this->_uniacid];

        //$member_growth = $this->level_model->memberGrowth($this->_uniacid);
        //创建时间搜索
        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['vip_time','between',"$start_time,$end_time"];
        }
        //等级搜索
        if(!empty($input['level'])){

            $dis[] = ['member_level','=',$input['level']];

        }else{

            $dis[] = ['member_level','>',0];

        }
        $where = [];
        //昵称搜索
        if(!empty($input['name'])){

            $where[] = ['nickName','like','%'.$input['name'].'%'];

            $where[] = ['phone','like','%'.$input['name'].'%'];

            $where[] = ['id','=',$input['name']];


        }
        //储值查询
        if(!empty($input['start_price'])&&!empty($input['end_price'])){

            $start_price = $input['start_price'];

            $end_price   = $input['end_price'];

            $dis[]       = ['balance','between',"$start_price,$end_price"];

        }

        $data = $this->user_model->dataList($dis,$input['limit'],$where);

        if(!empty($data['data'])){

            $level_model = new Level();

            foreach ($data['data'] as &$v){

                $v['level_title'] = $level_model->where(['id'=>$v['member_level']])->value('title');

                $list_form = $this->model->listForm($v['id']);

                $v['order_price'] = $list_form['price'];

                $v['order_count'] = $list_form['count'];

            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-15 11:21
     * @功能说明:会员等级详情
     */
    public function memberInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $level_model = new Level();

        //用户信息
        $data['user_info'] = $this->user_model->dataInfo($dis);

        $data['user_info']['level_title'] = $level_model->where(['id'=> $data['user_info']['member_level']])->value('title');
        //统计信息
        $data['form_list'] = $this->model->listForm($input['id']);

        $dis = [

            'user_id' => $input['id'],

        ];

        if($input['type']==1){
            //积分记录
            $data['data_list'] = $this->integral_model->integralList($dis,$input['limit']);

        }elseif($input['type']==2){

            $balance_model = new BalanceWater();
            //储值明细
            $data['data_list'] = $balance_model->indexList($dis,$input['limit']);

        }

        return $this->success($data);
    }




    /**
     * @author chenniang
     * @DataTime: 2020-07-15 13:24
     * @功能说明:
     */
    public function statusUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $member = $this->model->getMemberInfo($dis);

        $status = $member['status']==1?0:1;

        $data  = $this->model->memberUpdate($dis,['status'=>$status]);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-29 10:14
     * @功能说明:会员权限
     */
    public function memberAuth(){

        $persisson = new PermissionMember($this->_uniacid);

        $auth = $persisson->pAuth();

        return $this->success($auth);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-31 15:41
     * @功能说明:修改成长值
     */
    public function updateGrowth(){

        $input = $this->_input;

        $member = $this->model->memberInfo(['a.id'=>$input['id']]);

        $insert = [

            'uniacid' => $this->_uniacid,

            'user_id' => $member['user_id'],

            'controller' => $this->_user['admin_id'],

            'growth_add' => $input['growth'],

            'growth_before' => $member['growth'],

            'growth_after'  => $input['growth'],

            'status'     => 2,

            'type'       => 1,

            'controller_type' => 1
        ];
        //添加记录
        $this->growth_model->growthAdd($insert);

        $order = [

            'uniacid' => $this->_uniacid,

            'user_id' => $member['user_id'],

            'to_uid'  => 0
        ];
        //给用户修改成长值
        $res = $this->level_model->upUserRightsEnd(['growth'=>$input['growth']],$order);

        return $this->success($res);

    }

    /**
     * User: chenniang
     * Date: 2019-09-09 14:36
     * @return \think\Response
     * descption:佣金流水列表
     */
    public function waterList(){

        $input = $this->_param;

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['b.nickName','like',"%{$input['name']}%"];

            $where[] = ['g.phone','like',"%{$input['name']}%"];
        }

        if(!empty($input['thing_type'])){

            $dis[] = ['a.thing_type','=',$input['thing_type']];

        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];

        }

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.thing_type','in',[1,2,3]];

        $water_model = new \app\shop\model\AdminSellingWater();

        $data = $water_model->storedWaterList($dis,$where,$this->_param['limit']);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-08-24 14:29
     * @功能说明:出库导出
     */
    public function memberListExcel(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $where1 = [];
        //创建时间搜索
        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time   = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];
        }
        //等级搜索
        if(!empty($input['level'])){

            $growth_section = $this->level_model->getGrowth($input['level']);

            $start_growth   = $growth_section['start_growth'];

            $end_growth     = !empty($growth_section['end_growth'])?$growth_section['end_growth']-1:$growth_section['end_growth'];

            $dis[] = !empty($end_growth)?['a.growth','between',"$start_growth,$end_growth"]:['a.growth','>',"$start_growth"];

        }
        $where = [];
        //昵称搜索
        if(!empty($input['name'])){

            $where[] = ['b.nickName','like','%'.$input['name'].'%'];

            $where[] = ['b.id','=',$input['name']];

        }
        //储值查询
        if(!empty($input['start_price'])&&!empty($input['end_price'])){

            $start_price = $input['start_price'];

            $end_price   = $input['end_price'];

            $dis[]       = ['a.stored','between',"$start_price,$end_price"];

        }

        $data = $this->model->memberList($dis,$where1,$where,100000000);

        $name  ='会员列表';

        $header=[
            'ID',
            '手机号码',
            '用户昵称',
            '会员等级',
            '订单数量',
            '消费金额',
            '可用积分',
            '当前成长值',
            '储值余额',
            '成为会员时间',
            '会员有效期',

        ];


        $new_data = [];

        if(!empty($data['data'])){

            foreach ($data['data'] as $v){

                if(!empty($v['level_content'])){

                    $over_time = !empty($v['over_time'])?date('Y-m-d H:i:s',$v['over_time']):'终生有效';
                }else{

                    $over_time = '';
                }


                $info   = array();

                $info[] = $v['id'];

                $info[] = !empty($v['member_phone'])?$v['member_phone']:'';

                $info[] = $v['nickName'];

                $info[] = $v['level_content'];

                $info[] = $v['order_num'];

                $info[] = $v['price'];

                $info[] = $v['integral'];

                $info[] = $v['growth'];

                $info[] = $v['stored'];

                $info[] = !empty($v['level_content'])?date('Y-m-d H:i:s',$v['create_time']):'';

                $info[] = $over_time;

                $new_data[] = $info;

            }

        }

        $excel_model = new Excel();
        //返回数据
        $fileName = $excel_model->excelExport($name,$header,$new_data);

        return $this->success(1);

    }








}
